<?php

namespace App\Http\Controllers;

use App\Models\CaseBak;
use App\Models\CaseReport;
use App\Models\DoctorWorks;
use App\Models\HealthReminder;
use App\Models\Memos;
use App\Models\RelationshipCase;
use App\Models\ReportLabel;
use App\Models\Target;
use App\Models\TargetLog;
use Illuminate\Http\Request;
use App\Service\CaseDescribe;
use Illuminate\Support\Facades\Validator;

//对应前台>问诊->电话咨询->病情描述copy copy->新建
class CaseDescribeController extends BaseController
{
    /**
     * Get a validator for an incoming registration request.
     *
     * @param array $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'string|max:255',
            'sex' => 'required|numeric|min:1',
            'age' => 'required|numeric',
            'user_id' => 'required|numeric',
            'bloodType' => 'string',
            'allergy' => 'string',
            'diseasesHistory' => 'string',
            'picture' => 'string',
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 添加病例
     */
    public function create(Request $request)
    {
        $valid = $this->validator($request->all());
        if (count($valid->errors()) > 0) {
            return response()->json($valid->errors());
        }
        $request->allergy = $this->handelString($request->allergy);
        $request->diseases_history = $this->handelString($request->diseases_history);
        $request->picture = $this->handelString($request->picture);
        $dateTime = date("Y-m-d H:i:s");
        $data['name'] = $request->name ?? '';
        $data['sex'] = $request->sex ?? '';
        $data['age'] = $request->age ?? '';
        $data['id'] = $request->id ?? '';
        $data['user_id'] = $request->user_id ?? '';
        $data['relation'] = $request->relation ?? '';
        $data['bloodType'] = $request->bloodType ?? '';
        $data['allergy'] = $request->allergy ?json_encode($request->allergy): [];
        $data['diseases_history'] = $request->diseases_history ?json_encode($request->diseases_history): [];
        $data['picture'] =$request->picture ?json_encode($request->picture): [];
        $data['created_at'] = $dateTime;
        $data['updated_at'] = $dateTime;
        $obj = (new CaseDescribe())->Add($data);
        if ($obj) {
            if(isset($data['id']) && !empty($data['id'])){
                (new CaseBak())->add($data,$data['id']);
                return  $this->ResultJson(1, "编辑成功", []);
            }else{
                (new CaseBak())->add($data,$obj);
                return  $this->ResultJson(1, "添加成功", []);
            }
        }
    }

    /***
     * 病情描述提交
     */

    public function setCaseDescribe(Request $request)
    {
        $param = $request->input();
        if(empty($param['case_id'])){
            return  $this->ResultJson(0, "病例ID不能为空", []);
        }
        if(empty($param['describe'])){
            return  $this->ResultJson(1, "图片和描述不能为空!",[]);
        }
        $res = (new \App\Models\CaseDescribe())->updateCase($param);
        if($res){
            return  $this->ResultJson(1, "提交成功!", ["case_id"=>$param['case_id']]);
        }
    }
    



    /**
     * @param $str
     * @return array处理字符串编数组
     */
    public function handelString($str)
    {
        $left = trim($str, "[");
        $string = trim($left, "]");
        $array = explode(',', $string);
        return $array;
    }


    /****
     * 我的病例列表
     */

    public function getMyCase(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        $res = (new \App\Models\CaseDescribe())->getMyCase($param);
        return $this->ResultJson(1, '获取成功!', $res);
    }

    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 病例详情
     */
    public function getMyCaseDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, 'ID不能为空!', []);
        }
        $res = (new \App\Models\CaseDescribe())->getMyCaseDetail($param);
        return $this->ResultJson(1, '获取成功!', $res);

    }

    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 添加关联家属
     */
    public function relationshipCase(Request $request)
    {
        $param =  $request->input();
        if(empty($param['case_id'])){
            return $this->ResultJson(0, '病例ID不能为空!', []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        $res = (new RelationshipCase())->addRelation($param);
        if($res === true){
            return $this->ResultJson(1, '绑定成功!', []);
        }
        return $this->ResultJson(0, $res, []);
    }

    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 解除关联家属
     */
    public function cancleRelationshipCase(Request $request)
    {
        $param =  $request->input();
        if(empty($param['case_id'])){
            return $this->ResultJson(0, '病例ID不能为空!', []);
        }
        if(empty($param['user_id'])){
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        $res = (new RelationshipCase())->cancleRelation($param);
        if($res === true){
            return $this->ResultJson(1, '解除绑定成功!', []);
        }
        return $this->ResultJson(0, $res, []);
    }

    /***
     * @param Request $request
     * 添加用药提醒
     */
    public function addHealthReminder(Request $request)
    {
        $param = $request->input();
        if (!isset($param['id'])) {
            $result = $this->verify($param);
        } else {
            if(empty($param['id'])){
                return $this->ResultJson(0, 'ID不能为空!', []);
            }
            $result = true;
        }
        if ($result === true) {
            $res = (new HealthReminder())->addHealthReminder($param);
            if ($res == true) {
                return $this->ResultJson(1, "添加/编辑成功!", []);
            } else {
                return $this->ResultJson(0, "失败!", []);
            }
        }
        return $result;
    }

    /***
     * @param Request $request
     * 获取用药提醒列表
     */
    public function getHealthReminder(Request $request)
    {
        $param = $request->input();
//        if (empty($param['user_id'])) {
//            return $this->ResultJson(0, '用户id不能为空!', []);
//        }
//        if (empty($param['case_id'])) {
//            return $this->ResultJson(0, '案例id不能为空!', []);
//        }
        $res = (new HealthReminder())->getHealthReminder($param);
        return $this->ResultJson(1, "成功!", $res);
    }

    /***
     * @param Request $request
     * 获取用药提醒列表
     */
    public function getHealthReminderDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, 'ID不能为空!', []);
        }
        $res = (new HealthReminder())->getInfo($param['id']);
        if(!empty($res)){
            $res = $res->toArray();
        }
        return $this->ResultJson(1, "成功!", $res);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 删除用药提醒
     */
    public function delHealthReminder(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, 'ID不能为空!', []);
        }
        $res = (new HealthReminder())->del($param);
        if($res){
            return $this->ResultJson(1, "删除成功!", []);
        }
        return $this->ResultJson(0, "删除失败!", []);
    }
    
    
    
    
    
    /***
     * @param $res
     * @return bool|\Illuminate\Http\JsonResponse
     * 验证消息提醒
     */
    public function verify($res)
    {
        if (empty($res['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
//        if (empty($res['case_id'])) {
//            return $this->ResultJson(0, '病例id不能为空!', []);
//        }
        if (empty($res['use_type'])) {
            return $this->ResultJson(0, '设置类型不能为空!', []);
        }
        if (empty($res['name'])) {
            return $this->ResultJson(0, '名称不能为空!', []);
        }
        if (empty($res['frequency'])) {
            return $this->ResultJson(0, '用药频次不能为空!', []);
        }
        if (empty($res['take_time'])) {
            return $this->ResultJson(0, '吃药时间不能为空!', []);
        }
        if (empty($res['start_time'])) {
            return $this->ResultJson(0, '开始时间不能为空!', []);
        }
        if (empty($res['end_time'])) {
            return $this->ResultJson(0, '结束时间不能为空!', []);
        }
        if (empty($res['meal_type'])) {
            return $this->ResultJson(0, '饭前饭后不能为空!', []);
        }
        return true;
    }

     /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 添加病例文件
     */
    public function addCaseReport(Request $request)
    {
        $param = $request->input();
        $valid = $this->verifyCaseReport($param);
        if ($valid === true) {
            $obj = (new CaseReport())->doSetting($param);
            if ($obj === true) {
                if (isset($param['id']) && !empty($param['id'])) {
                    return $this->ResultJson(1, "编辑成功", []);
                } else {
                    return $this->ResultJson(1, "添加成功", []);
                }
            }
        }
        return $valid;
    }

    /****
     * @param Request $request
     * @return bool|\Illuminate\Http\JsonResponse
     * 添加标签
     */
    public function addReportLabel(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
        if (empty($param['label'])) {
            return $this->ResultJson(0, '标签名称不能为空!', []);
        }
        $obj = (new ReportLabel())->doSetting($param);
        if ($obj === true) {
            if (isset($param['id']) && !empty($param['id'])) {
                return $this->ResultJson(1, "编辑成功", []);
            } else {
                return $this->ResultJson(1, "添加成功", []);
            }
        }
    }


    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取病历标签列表
     */
    public function getReportLabelList(Request $request)
    {
        $res = $request->input();
//        if (empty($res['case_id'])) {
//            return $this->ResultJson(0, '病例ID不能为空!', []);
//        }
        if (empty($res['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        $list = (new ReportLabel())->getReportLabelList($res);
        return $this->ResultJson(1, '获取成功!', $list);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 删除标签
     */
    public function delReportLabel(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '文件夹id不能为空!', []);
        }
        $obj = (new ReportLabel())->delReportLabel($param);
        if ($obj) {
            return $this->ResultJson(1, "删除成功", []);
        }else{
            return $this->ResultJson(0, "删除失败", []);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 删除病例文件
     */
    public function delCaseReport(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '文件夹id不能为空!', []);
        }
        $obj = (new CaseReport())->delCaseReport($param);
        if ($obj) {
            return $this->ResultJson(1, "删除成功", []);
        }else{
            return $this->ResultJson(0, "删除失败", []);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 删除病例
     */
    public function delCaseDescribe(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '病例id不能为空!', []);
        }
        $obj = (new \App\Models\CaseDescribe())->delCaseDescribe($param);
        if ($obj) {
            return $this->ResultJson(1, "删除成功", []);
        }else{
            return $this->ResultJson(0, "删除失败", []);
        }
    }

    /***
     * @param $res
     * @return bool|\Illuminate\Http\JsonResponse
     * 验证病例数据
     */
    public function verifyCaseReport($res)
    {
        if (empty($res['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
        if (empty($res['case_id'])) {
            return $this->ResultJson(0, '病例ID不能为空!', []);
        }
        if (empty($res['images'])) {
            return $this->ResultJson(0, '病例文件不能为空!', []);
        }
        if (empty($res['ins_times'])) {
            return $this->ResultJson(0, '日期不能为空!', []);
        }
        if (empty($res['tags'])) {
            return $this->ResultJson(0, '标签名称不能为空!', []);
        }
        return true;
        
    }

    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取病历夹列表
     */
    public function getCaseReportList(Request $request)
    {
        $res = $request->input();
        if (empty($res['case_id'])) {
            return $this->ResultJson(0, '病例ID不能为空!', []);
        }
        $list = (new CaseReport())->getCaseReportList($res);
        return $this->ResultJson(1, '获取成功!', $list);
    }

    public function getCaseReportListAndroid(Request $request)
    {
        $res = $request->input();
        if (empty($res['case_id'])) {
            return $this->ResultJson(0, '病例ID不能为空!', []);
        }
        $list = (new CaseReport())->getCaseReportListAndroid($res);
        return $this->ResultJson(1, '获取成功!', $list);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 添加备忘录
     */
    public function addMemos(Request $request)
    {
        $param = $request->input();
        $valid = $this->validMemo($param);
        if (count($valid->errors()) > 0) {
            return response()->json($valid->errors());
        }
        $obj = (new Memos())->doSetting($param);
        if ($obj === true) {
            if (isset($param['id']) && !empty($param['id'])) {
                return $this->ResultJson(1, "编辑成功", []);
            } else {
                return $this->ResultJson(1, "添加成功", []);
            }
        }
        return $valid;
    }

    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取备忘录列表
     */
    public function getMemoList(Request $request)
    {
        $res = $request->input();
//        if (empty($res['case_id'])) {
//            return $this->ResultJson(0, '病例ID不能为空!', []);
//        }
        $list = (new Memos())->getMemoList($res);
        return $this->ResultJson(1, '获取成功!', $list);
    }

    public function getMemoListAndroid(Request $request)
    {
        $res = $request->input();
//        if (empty($res['case_id'])) {
//            return $this->ResultJson(0, '病例ID不能为空!', []);
//        }
        $list = (new Memos())->getMemoListAndroid($res);
        return $this->ResultJson(1, '获取成功!', $list);
    }

    protected function validMemo(array $data)
    {
        return Validator::make($data, [
            'content' => 'required|string|max:255',
//            'type' => 'required|numeric|min:1',
            'user_id' => 'required|numeric',
//            'case_id' => 'required|numeric',
            'ins_times' => 'required|string',
            'times' => 'required|string'
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 删除备忘录
     */
    public function delMemos(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '备忘录id不能为空!', []);
        }
        $obj = (new Memos())->delMemos($param);
        if ($obj) {
            return $this->ResultJson(1, "删除成功", []);
        }
    }

    public function detailMemos(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '备忘录id不能为空!', []);
        }
        $obj = (new Memos())->detailMemos($param);
        if ($obj) {
            return $this->ResultJson(1, "获取成功",$obj->toArray());
        }
    }





    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 添加指标
     */
    public function addTargets(Request $request)
    {
        $param = $request->input();
        $valid = $this->validTargets($param);
        if (count($valid->errors()) > 0) {
            return response()->json($valid->errors());
        }
        $obj = (new Target())->doSetting($param);
        if ($obj === true) {
            if (isset($param['id']) && !empty($param['id'])) {
                return $this->ResultJson(1, "编辑成功", []);
            } else {
                return $this->ResultJson(1, "添加成功", []);
            }
        }
        return $valid;
    }

    protected function validTargets(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
//            'values' => 'required|numeric',
            'user_id' => 'required|numeric',
//            'case_id' => 'required|numeric',
            'unit' => 'required|string',
//            'remark' => 'string'
        ]);
    }

    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取指标列表
     */
    public function getTargetsList(Request $request)
    {
        $res = $request->input();
//        if (empty($res['case_id'])) {
//            return $this->ResultJson(0, '病例ID不能为空!', []);
//        }
        $list = (new Target())->getTargetsList($res);
        return $this->ResultJson(1, '获取成功!', $list);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 删除指标
     */
    public function delTargets(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '指标id不能为空!', []);
        }
        $obj = (new Target())->delTargets($param);
        if ($obj) {
            return $this->ResultJson(1, "删除成功", []);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 添加指标
     */
    public function addTargetsLogs(Request $request)
    {
        $param = $request->input();
        $valid = $this->validTargetsLog($param);
        if (count($valid->errors()) > 0) {
            return response()->json($valid->errors());
        }
        $obj = (new TargetLog())->doSetting($param);
        if ($obj === true) {
            if (isset($param['id']) && !empty($param['id'])) {
                return $this->ResultJson(1, "编辑成功", []);
            } else {
                return $this->ResultJson(1, "添加成功", []);
            }
        }
        return $this->ResultJson(0, $obj, []);
    }

    protected function validTargetsLog(array $data)
    {
        return Validator::make($data, [
            'ins_times' => 'required|string',
            'target_id' => 'required|numeric',
            'user_id' => 'required|numeric',
//            'case_id' => 'required|numeric',
            'high' => 'required',
//            'low' => 'required',
//            'remark' => 'string'
        ]);
    }

    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取指标记录列表
     */
    public function getTargetsLogList(Request $request)
    {
        $res = $request->input();
//        if (empty($res['case_id'])) {
//            return $this->ResultJson(0, '病例ID不能为空!', []);
//        }
        $list = (new TargetLog())->getTargetsLogList($res);
        return $this->ResultJson(1, '获取成功!', $list);
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * android使用
     */
    public function getTargetsLogListAndroid(Request $request)
    {
        $res = $request->input();
//        if (empty($res['case_id'])) {
//            return $this->ResultJson(0, '病例ID不能为空!', []);
//        }
        $list = (new TargetLog())->getTargetsLogListAndroid($res);
        return $this->ResultJson(1, '获取成功!', $list);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 删除指标记录
     */
    public function delTargetsLog(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '指标记录id不能为空!', []);
        }
        $obj = (new TargetLog())->delTargetsLog($param);
        if ($obj) {
            return $this->ResultJson(1, "删除成功", []);
        }
    }


    /***
     * 工作室文章
     */
    public function getWorksArticle(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
        $list = (new DoctorWorks())->getWorksArticle($param);
        if (!empty($list)) {
            return $this->ResultJson(1, "获取成功", $list);
        }
        return $this->ResultJson(0, "暂无关注或者科室没有文章", []);
    }

    public function getTargetsStatisticalAndroid(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
//        if (empty($param['case_id'])) {
//            return $this->ResultJson(0, '病例id不能为空!', []);
//        }
        if (empty($param['target_id'])) {
            return $this->ResultJson(0, '标签id不能为空!', []);
        }
        $obj = (new TargetLog())->getTargetsStatisticalAndroid($param);
        if ($obj) {
            return $this->ResultJson(1, "获取成功", $obj);
        }

    }







    /**
     * 指标统计图
     */
    public function getTargetsStatistical(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
//        if (empty($param['case_id'])) {
//            return $this->ResultJson(0, '病例id不能为空!', []);
//        }
        if (empty($param['target_id'])) {
            return $this->ResultJson(0, '标签id不能为空!', []);
        }
        $obj = (new TargetLog())->getTargetsStatistical($param);
        if ($obj) {
            return $this->ResultJson(1, "获取成功", $obj);
        }
        
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取日历数据
     */
    public function getCalendarStatistical(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
        if (empty($param['case_id'])) {
            return $this->ResultJson(0, '病例id不能为空!', []);
        }
//        if (empty($param['target_id'])) {
//            return $this->ResultJson(0, '标签id不能为空!', []);
//        }
        $obj = (new TargetLog())->getCalendarStatistical($param);
        if ($obj) {
            return $this->ResultJson(1, "获取成功", $obj);
        }

    }




}
